{extend name='common/content' /}

{block name="content"}
<div class="admin-main layui-anim layui-anim-upbit">
    <!-- <fieldset class="layui-elem-field layui-field-title">
        <legend>{:lang('list')}</legend>
    </fieldset> -->
<!-- 表单搜索 开始 -->
<form class="layui-form layui-form-pane form-search" action="__SELF__" onsubmit="return false" method="get">

    <div class="layui-form-item layui-inline">
        <label class="layui-form-label" style="width:auto">名称</label>
        <div class="layui-input-inline">
            <input name="title" value="{$Think.get.title|default=''}" placeholder="名称" class="layui-input">
        </div>
    </div>

<!--     <div class="layui-form-item layui-inline">
        <label class="layui-form-label" style="width:auto">传承人名称</label>
        <div class="layui-input-inline">
            <input name="user_id" value="{$Think.get.user_id|default=''}" placeholder="传承人名称" class="layui-input">
        </div>
    </div> -->

<!--     <div class="layui-form-item layui-inline">
        <label class="layui-form-label" style="width:auto">所属地区</label>
        <div class="layui-input-inline">
            <select name='aid' class='layui-select'>
                <option value="">所属地区</option>
                {foreach $areaList as $val}
                {eq name='val.area_id' value='$Think.get.aid|default=0'}
                <option selected  value='{$val.area_id}'>{$val.spl}{$val.area_name}</option>
                {else/}
                <option  value='{$val.area_id}'>{$val.spl}{$val.area_name}</option>
                {/eq}
                {/foreach}
            </select>
        </div>
    </div> -->

<!--     <div class="layui-form-item layui-inline">
        <div class="layui-form-label">地区</div>
        <div class="layui-input-inline">
          <select name="province" id="province" lay-filter="province">
            {$province}
          </select>
        </div>
        <div class="layui-input-inline">
          <select name="city" id="city" lay-filter="city">
          </select>
        </div>
        <div class="layui-input-inline">
          <select name="county" id="county">
          </select>
        </div>
    </div> -->

    <div class="layui-form-item layui-inline">
        <div class="layui-input-inline">
            <button type="submit" class="btn btn-sm btn-white"><i class="fa fa-search"></i> 搜索</button>
        </div>
    </div>

</form>
<!-- 表单搜索 结束 -->
    <div class="demoTable">
<!--         <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="{:lang('pleaseEnter')}关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button> -->
        <!-- <a data-reload class="layui-btn">显示全部</a> -->
        <!-- <button type="button" class="layui-btn" id="statusAll">批量审核</button> -->
        <!-- <button type="button" class="layui-btn layui-btn-normal" id="statusCancelAll">取消审核</button> -->
        <!-- <button type="button" class="layui-btn" id="pushAll">批量提取</button> -->
        <!-- <button type="button" class="layui-btn layui-btn-normal" id="pushCancelAll">取消提取</button> -->
        <button type="button" class="layui-btn layui-btn-danger" id="delAll">批量删除</button>
        <!-- <a href="{:url('/').'client.html#/client/category/index.html?spm=m-290-230'}" class="layui-btn layui-btn-normal" style="float:right;">返回</a> -->
         <a href="{:url('/').'client.html#/client/'.MODULE_NAME.'/add.html'}?catid=<?php echo input('catid')?>&spm=m-290-230" class="layui-btn" style="float:right;margin-right: 15px;">{:lang('add')}</a>
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script>window.form.render();</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="title">
    <span style="{{d.title_style}}">{{d.title}}</span>
    {{# if(d.thumb){ }}<img src="/static/admin/images/image.gif" onmouseover="layer.tips('<img src={{d.thumb}}>',this,{tips: [1, '#fff']});" onmouseout="layer.closeAll();">{{# } }}
</script>
<!-- <script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="发布|隐藏" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script> -->
<script type="text/html" id="action">
<!--     {{# if(d.is_show==1){ }}
    <a href="{{d.url}}" target="_blank" class="layui-btn layui-btn-xs">预览</a>
    {{# } }}
    {{# if(d.status===0){ }}
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="已审核|未审核" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
    {{# } }} -->
<!--     {{# if(d.is_push){ }}
        <input type="checkbox" name="push" value="{{d.id}}" lay-skin="switch" lay-text="已提取|未提取" lay-filter="push" {{ d.push_status == 1 ? 'checked' : '' }}>
    {{# } }} -->
    <a href="{:url('/').'client.html#/client/'.MODULE_NAME.'/edit.html'}?id={{d.id}}&catid={{d.catid}}&spm=m-290-230" class="layui-btn layui-btn-xs">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use('table', function() {
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var tableIn = table.render({
            id: 'content',
            elem: '#list',
            url: '{:url("index")}?mod={isset(input("mod")) ? input("mod") : ""}',
            where:{catid:'{:input("catid")}',aid:"{:input('aid')}",title:"{:input('title', '')}",province:"{:input('province', '')}",city:"{:input('city', '')}",county:"{:input('county', '')}"},
            done: function(res, curr, count){
                // console.log(res);return;
                //地区选择
                if("{:input('province', '')}"){
                    $("#province").html(res.province);
                    $("#city").html(res.city);
                    $("#county").html(res.county);
                    form.render('select');                
                }
                if("{:input('city', '')}"){
                    $("#city").html(res.city);
                    $("#county").html(res.county);
                    form.render('select');                
                }
                if("{:input('county', '')}"){
                    $("#county").html(res.county);
                    form.render('select');                
                }

                //县级地区限制
                var aid = "{:session('c_user.aid')}";
                // console.log(aid);return;
                // $("select#province option").each(function(i,v){
                //     if($(v).val() == aid){
                //         // console.log($(this));
                //         $(this).attr('selected',true);form.render('select');
                //     }
                // })
                if(aid != 1){
                    $("#province").find("option[value="+aid+"]").attr("selected",true);
                    $("#province").find("option[value!="+aid+"]").attr("disabled",true);
                    $("#city").html(res.city);
                    $("#county").html(res.county);
                    form.render('select');
                }
            },
            method: 'post',
            page: true,
            cols: [[
                {type: "checkbox", fixed: true},
                {field: 'id', title: '{:lang("id")}', width: 80, fixed: true},
                {field: 'title', title: '名称', width: 380, templet: '#title'},
                {field: 'username', title: '发布人', width: 280},
                // {field: 'hits',  title: '{:lang("hit")}', width: 80},
                {field: 'status1', align: 'center', title: '{:lang("status")}', width: 100},
                {field: 'area_name',  title: '来源地区', width: 250},
                {field: 'createtime', title: '{:lang("add")}{:lang("time")}', width: 160},
                // {field: 'updatetime', title: '{:lang("edit")}{:lang("time")}', width: 160},
                {field: 'sort', align: 'center', title: '{:lang("order")}', width: 80, templet: '#order'},
                // {field: 'push', title: '推荐属性', width: 200},
                {width: 415, align: 'center', toolbar: '#action',title:'操作'}
            ]],
            limit: 20
        });

        //地区
        form.on('select(province)', function(obj){
            // console.log(obj);return;
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = obj.value;
            $.post('{:url("admin/common/_ajaxGetCity")}',{'id':id},function (res) {
            // console.log(id);return;
                layer.close(loading);
                if (res) {
                    $("#city").html(res);
                    if(id == 0){
                        $("#county").html(res);
                    }
                    form.render('select');
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        form.on('select(city)', function(obj){
            // console.log(obj);return;
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = obj.value;
            $.post('{:url("admin/common/_ajaxGetCity")}',{'id':id},function (res) {
            // console.log(res);return;
                layer.close(loading);
                if (res) {
                    $("#county").html(res);
                    form.render('select');
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        //设置状态
        form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('{:url("editState")}',{'id':id,'status':status},function (res) {
                layer.close(loading);
                if (res.status==1) {
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        //设置状态
        form.on('switch(push)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var push = obj.elem.checked===true?1:0;
            $.post('{:url("editPush")}',{'id':id,'push':push},function (res) {
                layer.close(loading);
                if (res.status==1) {
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', {icon: 0});
                return;
            }
            tableIn.reload({ page: {page: 1}, where: {key: key,catid:'{:input("catid")}',aid:"{:input('aid')}"} });
        });
        $('body').on('blur','.list_order',function() {
            var id = $(this).attr('data-id');
            var sort = $(this).val();
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("listorder")}',{id:id,sort:sort,catid:'{:input("catid")}'},function(res){
                layer.close(loading);
                if(res.code === 1){
                    layer.msg(res.msg, {time: 1000, icon: 1}, function () {
                        location.href = res.url;
                    });
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                }
            })
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除该数据吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('listDel')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload({where:{catid:'{:input("catid")}'}});
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $('#delAll').click(function(){
            layer.confirm('确认要删除选中的数据吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('content'); //content即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要删除的数据！', {time: 1000, icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delAll')}", {ids: ids,catid:'{:input("catid")}'}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload({where:{catid:'{:input("catid")}'}});
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })

        $('#statusAll').click(function(){
            layer.confirm('确认要审核选中的数据吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('content'); //content即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要审核的数据！', {time: 1000, icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('statusAll')}", {ids: ids,catid:'{:input("catid")}'}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload({where:{catid:'{:input("catid")}'}});
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })

        $('#statusCancelAll').click(function(){
            layer.confirm('确认要取消审核选中的数据吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('content'); //content即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要取消审核的数据！', {time: 1000, icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('statusCancelAll')}", {ids: ids,catid:'{:input("catid")}'}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload({where:{catid:'{:input("catid")}'}});
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })

        $('#pushAll').click(function(){
            layer.confirm('确认要提取选中的数据吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('content'); //content即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要提取的数据！', {time: 1000, icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('pushAll')}", {ids: ids,catid:'{:input("catid")}'}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload({where:{catid:'{:input("catid")}'}});
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })

        $('#pushCancelAll').click(function(){
            layer.confirm('确认要取消提取选中的数据吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('content'); //content即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要取消提取的数据！', {time: 1000, icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('pushCancelAll')}", {ids: ids,catid:'{:input("catid")}'}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload({where:{catid:'{:input("catid")}'}});
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })

    });
</script>
{/block}